import sdk from '../core/index';
import { config, configKey } from "../config";
import { analysisIntent } from "../core/tool/baseScene";
import { mathType } from "../core/tool/face";
import { quality } from "../shared/face/enums";
import { Icard2 } from "../core/tool/Icard2";
import { TalentService } from "../service/TalentService";
import { ModernTalentService } from "../service/ModernTalentService";

/**
 * 血统组件 - 现代化组件，使用依赖注入
 * 职责：
 * 1. 管理血统列表状态
 * 2. 处理用户血统交互
 * 3. 显示血统信息
 * 4. 通过注入的ModernTalentService处理业务逻辑
 */
interface cfg_Talent {
    id: string;
    level: number;
    quality: quality;
    exp_now: number,
    exp_max: number
}

import component from "../core/tool/component";
export default class talent_component extends component {
    private _modernTalentService: ModernTalentService;
    list: cfg_Talent[] = [];
    
    constructor() {
        super();
        // 使用依赖注入获取ModernTalentService（_开头，不会被持久化）
        this._modernTalentService = this.getService<ModernTalentService>('ModernTalentService');
    }
    
    init() {
        // 初始化血统组件
    }
    client_main() {
        let card = sdk.tool.card.create();
        card.title('✡我的血统');
        if (this.list.length == 0) {
            card.l('血统空空如也哦~');
        } else {
            this.list.forEach((item, index) => {
                let cfg = this.getCfg(item.id);
                let table = cfg.table.split('\n');
                let table_now = table[item.level - 1];
                card.l(`[${index + 1}]${quality[item.quality]}·${cfg.name}(${table_now})`);
            });
        }
        this.registerTalentButtonIntents(card);
        this.client_text(card.getText());
    }
    getTalentNames(): string[] {
        return this.list.map(item => {
            let cfg = this.getCfg(item.id);
            let table = cfg.table.split('\n');
            let table_now = table[item.level - 1];
            return table_now;
        });
    }
    client_look(data: analysisIntent) {
        let index = sdk.tool.common.cover_number(data.Message.content);
        if (!index.k1) {
            data.player.client_text('请输入血统ID');
            return;
        }
        let item = this.list[index.k1 - 1];
        if (!item) {
            data.player.client_text('血统不存在');
            return;
        }
        let card = sdk.tool.card.create2();
        card.title('✡我的血统');
        sdk.prop.client_talent(item.id, data.player, card, false);
        data.player.client_img('panel_common', card.resData);
    }
    client_del(data: analysisIntent) {
        let index = sdk.tool.common.cover_number(data.Message.content);
        if (!index.k1) {
            data.player.client_text('请输入血统ID');
            return;
        }
        let item = this.list[index.k1 - 1];
        if (!item) {
            data.player.client_text('血统不存在');
            return;
        }
        if(this.list.length == 1){
            data.player.client_text('至少保留一个血统');
            return;
        }
        this.list.splice(index.k1 - 1, 1);
        data.player.client_text('血统已废弃');
        this.client_main();
        this.fighting_end();
    }
    private getCfg(id: string): any {
        let cfg = config.get(configKey.配置_血统)?.get(id);
        if (!cfg) {
            debugger;
            return;
        }
        return cfg;
    }
    getItem(id: string): cfg_Talent | undefined {
        return this.list.find(item => item.id === id);
    }
    getAttribute(key: string): number {
        let baseValue = 0;
        this.list.forEach(item => {
            let cfg = this.getCfg(item.id);
            if (!cfg.attribute) {
                debugger;
                return;
            }
            let val = sdk.tool.common.getCfgAttributeVal(cfg.attribute, key);
            baseValue += (val + (item.quality / 10) * val) * item.level;
        });
        return baseValue;
    }
    private getMaxLevel(id: string): number {
        let cfg = this.getCfg(id);
        if (!cfg) {
            debugger;
            return 0;
        }
        return cfg.table.split('\n').length;
    }
    /**
     * 添加经验
     * @param exp 经验值
     */
    addExp(exp: number) {
        const result = this._modernTalentService.addExperience(this.list, exp);
        
        if (!result.success) {
            this.client_text(result.message);
            return;
        }
        
        this.list = result.newTalentList;
        
        if (result.levelUpMessages.length > 0) {
            result.levelUpMessages.forEach(msg => {
                this.client_text(msg);
            });
            this.fighting_end();
        }
    }
    // 注册血统按钮意图的通用方法
    private registerTalentButtonIntents(card: any): void {
        const buttonConfigs = [
            { text: '查看', action: 'client_look', mathType: mathType.开头匹配 },
            { text: '废弃', action: 'client_del', mathType: mathType.开头匹配 }
        ];
        
        this.registerButtonIntents(card, buttonConfigs);
    }
}

